package app

import (
	"github.com/pkg/errors"
	"github.com/spf13/viper"
	"guns-excel/internal/pkg/app"
	"guns-excel/internal/pkg/transports/http"
)

type Options struct {
	Name string // 服务名称
}

func NewOptions(v *viper.Viper) (*Options, error) {
	var err error
	o := new(Options)
	if err = v.UnmarshalKey("app", o); err != nil {
		return nil, err
	}
	return o, nil
}

// 实例化应用程序
func NewApp(o *Options, hs *http.Server) (*app.Application, error) {
	app, err := app.New(o.Name, hs)
	if err != nil {
		return nil, errors.Wrap(err, "实例化应用程序失败")
	}
	return app, nil
}
